package com.itheima.controller;

import com.itheima.project.*;
import com.itheima.project.costomannotation.log ;
import com.itheima.project.emp.Emp;
import com.itheima.project.log.LoginInfo;
import com.itheima.filter.service.inter.EmpService;
import com.itheima.util.JwtUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/login")
@RestController
public class LoginController {

    @Resource
    private EmpService empService;


    @log
    @PostMapping
    public Result login(@RequestBody LoginInfo emp) {
        log.info("正在登陆" + emp);
        Emp e = empService.login(emp);


        if (e != null) {
            Map<String, Object> clams = new HashMap<>();
            clams.put("id", e.getId());
            clams.put("name", e.getName());
            clams.put("username", e.getUsername());
            String jwt = JwtUtil.getJwt(clams);
            clams.put("token",jwt);
            return Result.success(clams);
        }

        return Result.error("账号密码错误");
    }

}
